package org.phoenix.mall.admin.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.phoenix.mall.admin.common.pojo.bo.GoodsBO;
import org.phoenix.mall.admin.common.utils.JsonUtil;
import org.phoenix.mall.admin.common.utils.PagedGridResult;
import org.phoenix.mall.admin.common.utils.PhoenixResponseJSON;
import org.phoenix.mall.admin.config.annotations.AuthRequired;
import org.phoenix.mall.admin.pojo.Goods;
import org.phoenix.mall.admin.service.GoodsService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Api(value = "商品管理", tags = {"商品管理API"})
@RestController
@RequestMapping("goods")
@AuthRequired
public class GoodsController {

    @Resource
    private GoodsService goodsService;

    @ApiOperation(value = "上传商品图片", notes = "上传商品图片", httpMethod = "POST")
    @PostMapping(value = "/pic/upload")
    public PhoenixResponseJSON upload(@RequestParam MultipartFile file) {
        String origName = file.getOriginalFilename();
        String extName = origName.substring(origName.lastIndexOf("."));
        String fileName = UUID.randomUUID().toString().replaceAll("-", "") + extName;

        String filePath = "/Users/xuchaotong/IdeaProjects/images/phoenixmall/";
        File targetFile = new File(filePath + fileName);
        try {
            file.transferTo(targetFile);
        } catch (IOException e) {
            e.printStackTrace();
        }

        Map<String, String> reMap = new HashMap<>();
        reMap.put("imagePath", "/images/" + fileName);
        return PhoenixResponseJSON.ok(reMap);
    }

    @ApiOperation(value = "添加商品", notes = "添加商品", httpMethod = "POST")
    @PostMapping(value = "")
    public PhoenixResponseJSON add(@RequestBody GoodsBO goodsBO) {
        Goods goods = new Goods();
        goods.setName(goodsBO.getName());
        goods.setPrice(goodsBO.getPrice());
        goods.setWeight(goodsBO.getWeight());
        goods.setStockQuantity(goodsBO.getStockQuantity());
        goods.setIntroduce(goodsBO.getIntroduce());
        goods.setTopCategoryId(Integer.valueOf(goodsBO.getCategoryIds().split(",")[0]));
        goods.setParentCategoryId(Integer.valueOf(goodsBO.getCategoryIds().split(",")[1]));
        goods.setCategoryId(Integer.valueOf(goodsBO.getCategoryIds().split(",")[2]));
        goods.setImages(goodsBO.getImages());
        goods.setAttributes(JsonUtil.objectToJson(goodsBO.getAttrs()));
        goods.setCreateTime(new Date());
        goods.setUpdateTime(new Date());

        int count = goodsService.add(goods);
        if (count == 0) {
            return PhoenixResponseJSON.errorMsg("添加商品出错！");
        }
        return PhoenixResponseJSON.ok();
    }

    @ApiOperation(value = "获取商品信息列表", notes = "获取商品信息列表", httpMethod = "GET")
    @GetMapping("")
    public PhoenixResponseJSON list(@ApiParam(name = "query", value = "查询条件")
                                    @RequestParam String query,
                                    @ApiParam(name = "pagenum", value = "页码")
                                    @RequestParam Integer pagenum,
                                    @ApiParam(name = "pagesize", value = "每页显示记录数")
                                    @RequestParam Integer pagesize) {


        if (pagenum == null) {
            pagenum = 1;
        }

        if (pagesize == null) {
            pagesize = 2;
        }
        PagedGridResult pagedGridResult = goodsService.getGoodsListByPage(pagenum, pagesize);

        return PhoenixResponseJSON.ok(pagedGridResult);
    }

}
